package com.ima;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.dev.ColorTextButton;
import com.dev.GUI;
import com.dev.IconViewClose;
import com.dev.ImageManipulationTab;
import com.dev.MainActivity;
import com.dev.ShowSoftInput;
import com.dev.TabData;
import com.dev.UnderlineSingleLineEditor;
import com.log;
import java.io.File;
import java.io.FileOutputStream;

public class QuerySaveImage extends LinearLayout implements OnClickListener,OnEditorActionListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	LinearLayout titlebar;
	TextView title;
	TextView row1_title;
	UnderlineSingleLineEditor row1_editor;
	IconViewClose close;
	ShowSoftInput ssi;
	MainActivity ma;
	LinearLayout row1;
	LinearLayout bts;
	ColorTextButton tbSure;
	int intend;
    public QuerySaveImage(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(1);
		setBackground(new GUI.BorderDrawable());
		int padding=9;
		int textSize=(int)(13*ma.dp);
		close=new IconViewClose(ma);
		close.setOnClickListener(this);
		ssi=new ShowSoftInput(ma);
		ssi.setOnClickListener(this);
		title=new TextView(ma);
		title.setPadding(padding,0,0,0);
		title.setGravity(16);
		title.getPaint().setTextSize(textSize);
		titlebar=new LinearLayout(ma);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.setBackground(new GUI.BorderDrawable());
		titlebar.addView(title,new LayoutParams(0,-1,1f));
		titlebar.addView(ssi);
		titlebar.addView(close);
		row1_title=new TextView(ma);
		row1_title.getPaint().setTextSize(textSize);
		row1_title.setGravity(16);
		row1_title.setPadding(padding,0,0,0);
		row1_title.setText("文件名：");
		row1_editor=new UnderlineSingleLineEditor(ma);
		row1_editor.setPadding(0,0,0,0);
		row1_editor.setOnEditorActionListener(this);
		row1=new LinearLayout(ma);
		row1.setOrientation(0);
		row1.addView(row1_title,-2,-1);
		row1.addView(row1_editor,new LayoutParams(0,-1,1f));
		tbSure=new ColorTextButton(ma);
		tbSure.setText("创建文件并保存");
		tbSure.setOnClickListener(this);
		bts=new LinearLayout(ma);
		bts.setOrientation(0);
		bts.addView(tbSure,-1,-1);
		LayoutParams titlebarlp=new LayoutParams(-1,ma.dialogTitleBarHeight);
		addView(titlebar,titlebarlp);
		addView(row1,titlebarlp);
		addView(bts,titlebarlp);
	}
	ImageManipulationTab imt;
	public void show(int intend,ImageManipulationTab imt){
		this.intend=intend;
		this.imt=imt;
		wlp.width=ma.wp*4/5;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}

	@Override
	public boolean onEditorAction(TextView textView, int p, KeyEvent keyEvent) {
		if(textView==row1_editor){
			onClick(tbSure);
		}
		return false;
	}
	@Override
	public void onClick(View view) {
		if(view==close){
			ma.wm.removeView(this);
		}else if(view==tbSure){
			String usrinput=row1_editor.getText().toString();
			if(usrinput.contains("/")){
				Toast.makeText(ma,"不能含有字符'/'",0).show();
			}else if(Environment.isExternalStorageManager()){
				File file=new File(ma.workDir,usrinput);
				if(file.exists()){
					Toast.makeText(ma,"已存在同名文件",0).show();
				}else{
					try{
						if(file.createNewFile()){
							FileOutputStream fos=new FileOutputStream(file);
							imt.imp.cv.image.compress(Bitmap.CompressFormat.PNG,100,fos);
							fos.close();
							ma.wm.removeView(this);
							Toast.makeText(ma,"已保存文件",0).show();
							TabData.ImageManipulation im=new TabData.ImageManipulation();
							im.src=file;
							imt.im=im;
						}else{
							Toast.makeText(ma,"创建文件失败",0).show();
						}
					}catch(Exception e){
						log.alert(e.toString());
					}
				}

			}else{
				Toast.makeText(ma,"尚未获得文件读写权限",0).show();
			}
		}
	}

	@Override
	protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
		row1_editor.requestFocus();
		return true;
	}
}
